package com.jp.knowledge.my.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.jp.knowledge.R;
import com.jp.knowledge.activity.CompanyBaseAcytivity;
import com.jp.knowledge.fragment.ConstactsFragment;
import com.jp.knowledge.my.model.OrganizaModel;

import java.util.ArrayList;
import java.util.List;

/**
 * 团队通通讯录
 *
 * @author gzejia 978862664@qq.com
 */
public class CompanyAddressBookActivity extends CompanyBaseAcytivity {

	private List<OrganizaModel> companys;

	private PopupWindow moreBtnPopupWindow;
	private View popWindowView;
	private TextView invitePeopleBtn;
	private View line1;
	private TextView departmentBtn;
	private View line2;
	private TextView companyFileBtn;

	@Override
	protected String getTitleString() {
		return "通讯录";
	}

	@Override
	protected void init() {
		super.init();
		initPopupWindow();
		rightBtn.setText("组织架构");
		rightBtn.setOnClickListener(this);
		rightBtn.setVisibility(View.GONE);
		rightIcon.setVisibility(View.VISIBLE);
		rightIcon.setImageResource(R.mipmap.guanzhugengduo);
		rightIcon.setOnClickListener(this);
		companys = getCompanys();
		hideKeyboard();
		viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
			@Override
			public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
			}

			@Override
			public void onPageScrollStateChanged(int state) {
			}

			@Override
			public void onPageSelected(int position) {
				hideKeyboard();
				setPopupWindow(companys.get(position).getCompanyAdmin() == 1);
			}
		});

		try {
			setPopupWindow(companys.get(viewPager.getCurrentItem()).getCompanyAdmin() == 1);
		} catch (Exception e) {
			setPopupWindow(false);
		}
		scrollToCompany(getIntent().getStringExtra("companyId"));
	}

	private void setPopupWindow(boolean showMore) {
		line1.setVisibility(showMore ? View.VISIBLE : View.GONE);
		departmentBtn.setVisibility(showMore ? View.VISIBLE : View.GONE);
		line2.setVisibility(showMore ? View.VISIBLE : View.GONE);
		companyFileBtn.setVisibility(showMore ? View.VISIBLE : View.GONE);
	}

	private void initPopupWindow() {
		popWindowView = View.inflate(mContext, R.layout.poput_more, null);
		invitePeopleBtn = (TextView) popWindowView.findViewById(R.id.pop_showImg);
		line1 = popWindowView.findViewById(R.id.line1);
		departmentBtn = (TextView) popWindowView.findViewById(R.id.pop_collection);
		line2 = popWindowView.findViewById(R.id.line2);
		companyFileBtn = (TextView) popWindowView.findViewById(R.id.pop_share);
		invitePeopleBtn.setText("邀请成员");
		departmentBtn.setText("部门管理");
		companyFileBtn.setText("企业档案");
		invitePeopleBtn.setCompoundDrawablesWithIntrinsicBounds(R.mipmap.invite_yaoqingtongshi, 0, 0, 0);
		departmentBtn.setCompoundDrawablesWithIntrinsicBounds(R.mipmap.invite_departmentmanagement, 0, 0, 0);
		companyFileBtn.setCompoundDrawablesWithIntrinsicBounds(R.mipmap.invite_enterprisefile, 0, 0, 0);
		invitePeopleBtn.setOnClickListener(this);
		departmentBtn.setOnClickListener(this);
		companyFileBtn.setOnClickListener(this);
		invitePeopleBtn.setTag(false);
		departmentBtn.setTag(false);

		popWindowView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
		moreBtnPopupWindow = new PopupWindow(popWindowView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
		moreBtnPopupWindow.setOutsideTouchable(true);
		moreBtnPopupWindow.setBackgroundDrawable(new ColorDrawable(0x00000000));
	}

	@Override
	protected void onNewIntent(Intent intent) {
		super.onNewIntent(intent);
		scrollToCompany(intent.getStringExtra("companyId"));
	}

	private void scrollToCompany(String companyId) {
		if (companys == null || companys.size() <= 1 || companyId == null)
			return;
		for (int i = 0, j = companys.size(); i < j; i++) {
			if (companyId.equals(companys.get(i).getCompanyId())) {
				viewPager.setCurrentItem(i);
				break;
			}
		}
	}

	@Override
	protected Fragment getFragment(OrganizaModel company) {
		return new ConstactsFragment().newInstance(company);
	}

	@Override
	protected void setTabNames(List<OrganizaModel> companys) {
		int size = companys.size();
		if (size == 0)
			tabView.setVisibility(View.GONE);
		else
			tabView.setVisibility(View.VISIBLE);
		List<String> tabNames = new ArrayList<>();
		for (OrganizaModel organizaModel : companys)
			tabNames.add(organizaModel.getCompanyName());
		tabView.setTabItems(tabNames);
	}

	@Override
	public void onClick(View v) {
		if (v == rightBtn && companys != null && companys.size() > 0) {
			int position = viewPager.getCurrentItem();
			OrganizaModel company = companys.get(position);
			Intent intent = new Intent(mContext, DepartmentManagerActivity.class);
			intent.putExtra("company", company);
			startActivity(intent);
		} else if (v == rightIcon) {
			popWindowView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
			moreBtnPopupWindow.showAsDropDown(rightIcon,
					-popWindowView.getMeasuredWidth() + rightIcon.getWidth(), 0);
		} else if (v == invitePeopleBtn && companys != null && companys.size() > 0) {
			startActivity(new Intent(mContext, InvitePeopleActivity.class)
					.putExtra("companyId", companys.get(viewPager.getCurrentItem()).getCompanyId()));
			moreBtnPopupWindow.dismiss();
		} else if (v == departmentBtn) {
			onClick(rightBtn);
			moreBtnPopupWindow.dismiss();
		} else if (v == companyFileBtn && companys != null && companys.size() > 0) {
			String companyId = companys.get(viewPager.getCurrentItem()).getCompanyId();
			int companyType = companys.get(viewPager.getCurrentItem()).getCompanyType();
			startActivity(new Intent(mContext, OrganizeInfoActivity.class)
					.putExtra("companyId", companyId).putExtra("companyType", companyType));
			moreBtnPopupWindow.dismiss();
		} else {
			super.onClick(v);
		}
	}

	@Override
	public void finish() {
		hideKeyboard();
		super.finish();
	}

	private void hideKeyboard() {
		InputMethodManager imm = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
		if (imm.isActive() && this.getCurrentFocus() != null)
			if (this.getCurrentFocus().getWindowToken() != null)
				imm.hideSoftInputFromWindow(this.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
	}
}
